unit frmdiff;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, vg_scene, vg_listbox, vg_controls, vg_layouts, vg_memo ,diff,
  vg_objects;

type
  TFormDiff = class(TForm)
    vgScene1: TvgScene;
    Root1: TvgBackground;
    Memo1: TvgMemo;
    Memo2: TvgMemo;
    Button1: TvgButton;
    StringListBox1: TvgStringListBox;
    lblchanges: TvgText;
    lbldiffcount: TvgText;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    diff:TDiff;
    { Public declarations }
  end;

var
  FormDiff: TFormDiff;

implementation

{$R *.dfm}

procedure TFormDiff.Button1Click(Sender: TObject);
var
  s1,s2:widestring;
  i,idx:integer;
  cr:TCompareRec;
//  o1,o2:string;
  o1,o2:tstringlist;
  s,o:string;
  lastkind:TChangeKind;
  lastindex:integer;
  lastchr1,lastchr2:widestring;
  changes:integer;
  middle:integer;
begin
  s1:=memo1.lines.text;
  s2:=memo2.lines.text;
  diff.Execute(pwidechar(s1),pwidechar(s2),length(s1),length(s2));
  stringlistbox1.items.clear;
//  stringlistbox2.clear;
  o1:=tstringlist.create;
  o2:=tstringlist.create;


  changes:=0;
  for i:=0 to diff.Count-1 do begin
    cr:=diff.Compares[i];
    o:='';
    if cr.Kind=ckNone then continue;

      inc(changes);
      idx:=cr.oldindex2;
      case cr.kind of
        ckAdd:o:='ADD '+cr.chr2;
        ckModify: o:=o+cr.chr1+'>'+cr.chr2;
        ckDelete: o:='DEL ';
      end;
    if (lastkind=cr.kind) and (cr.oldindex1=lastindex+1) then begin
      if ckdelete=cr.kind then o:='';
      if ckmodify=cr.kind then begin
        s:=o1[o1.count-1];
        middle:=(length(s)-10) div 2;
        s:=copy(s,10,middle)+cr.chr1 +'>'+
        copy(s,11+middle,length(s))+cr.chr2;
        o:='';
        o1[o1.count-1]:=s;
      end;
    end;

    if (lastkind=cr.kind) and (cr.oldindex1=lastindex) then begin
      if ckAdd=cr.Kind then begin
        o1[o1.count-1]:=o1[o1.count-1]+cr.chr2;
        o:='';
      end;
    end;

    lastkind:=cr.kind;
    lastindex:=cr.oldindex1;

    if o<>'' then o1.add(format('%.08d ',[idx])+o);
    {

    if cr.Kind = ckAdd then o1.add('ADD " "'+inttostr(cr.oldIndex1))
    else o1.add(cr.chr1+inttostr(cr.oldIndex1));


    if cr.Kind = ckDelete then o2.add('DELETE " "'+inttostr(cr.oldIndex2))
    else o2.add(cr.chr2+inttostr(cr.oldIndex2));
    }

  end;
  stringlistbox1.Items.AddStrings(o1);
//  stringlistbox2.Items.AddStrings(o2);
  o1.free;
  o2.free;
  lblchanges.text:=inttostr(changes);
  lbldiffcount.text:=inttostr(stringlistbox1.Count);
end;

procedure TFormDiff.FormCreate(Sender: TObject);
begin
  diff:=TDiff.create(nil);
end;

procedure TFormDiff.FormDestroy(Sender: TObject);
begin
  diff.Free;
end;

end.
